package pl.edu.agh.io.gpstracker.client.widget;

import java.util.LinkedHashMap;

import pl.edu.agh.io.gpstracker.client.action.SaveUserAction;
import pl.edu.agh.io.gpstracker.client.dto.UserDto;
import pl.edu.agh.io.gpstracker.client.dto.UserType;
import pl.edu.agh.io.gpstracker.client.result.VoidResult;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;

/**
 * 
 * @author wkedzior
 * 
 */
public class RegisterWidget extends GenericWidget {

	private static final String PASSWORD = "PASSWORD";
	private static final String LOGIN = "LOGIN";
	private static final String SURNAME = "SURNAME";
	private static final String NAME = "NAME";
	private static final String TYPE = "TYPE";

	private DynamicForm form;
	private IButton register;
	

	public RegisterWidget() {
	}

	@Override
	protected void onInit() {
		super.onInit();
		createWidget();
	}

	/**
	 * 
	 */
	private void createWidget() {
		form = new DynamicForm();
		// moze fabryka ?
		TextItem name = new TextItem(NAME, "Imie");
		name.setRequired(true);
		TextItem surname = new TextItem(SURNAME, "Nazwisko");
		surname.setRequired(true);
		TextItem login = new TextItem(LOGIN, "Login");
		login.setRequired(true);
		PasswordItem password = new PasswordItem(PASSWORD, "Haslo" 	);
		password.setRequired(true);
		
		LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();
		values.put("0", "Uzytkownik mobilny" );
		values.put( "1", "Uzytkownik webowy");

		SelectItem selectItem = new SelectItem(TYPE, "Typ");
		selectItem.setValueMap(values);
		selectItem.setRequired(true);
		form.setFields(name, surname, login, password, selectItem);

		HLayout bu = new HLayout();
		register = new IButton("Zarejestruj");
		register.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (form.validate()) {
					addNewUser();
				}
			}
		});
		bu.setMargin(10);
		bu.addMember(register);
		
		setMembers(form, bu);
	}

	/**
	 * 
	 * @return
	 */
	private UserDto createModel() {
		UserDto user = new UserDto();
		user.setLogin(form.getValueAsString(LOGIN));
		user.setName(form.getValueAsString(NAME));
		user.setPassword(form.getValueAsString(PASSWORD));
		user.setSurename(form.getValueAsString(SURNAME));
		// poprawic!!! masakra jakas
		user.setType((short) UserType.recognizeType(
				Short.parseShort(form.getValueAsString(TYPE))).getValue());
		return user;
	}

	/**
	 * 
	 */
	private void addNewUser() {
		dispatcher.execute(new SaveUserAction(createModel()),
				new AsyncCallback<VoidResult>() {

					@Override
					public void onFailure(Throwable caught) {
						SC.say(caught.toString());

					}

					@Override
					public void onSuccess(VoidResult result) {
						SC.say("Uzytkownik " + form.getValueAsString(LOGIN)
								+ " zostal poprawni zapisany!");
						form.clearValues();
					}
				});
	}
}
